//
//  DYCreateSignatureViewController.m
//  DaYing
//
//  Created by DaYing on 2024/1/13.
//  Copyright © 2024 DaYing. All rights reserved.
//

#import "DYCreateSignatureViewController.h"

#import "DYCreateSignatureTextCell.h"
#import "DYLotteryDecisionButtonCell.h"

#import "DYSignatureViewController.h"
#import "DYCreateSignatureToolBar.h"

@interface DYCreateSignatureViewController () <UIScrollViewDelegate>

@property (nonatomic,copy) NSString *signatureTitle;
@property (nonatomic,strong) NSMutableArray *signatureList;
@property (nonatomic,strong) DYCreateSignatureToolBar *toolBar; //提交按钮bar

@end

@implementation DYCreateSignatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNavigationItemTitleViewWithTitle:@"创建签文"];

    [self.tableView registerNib:[UINib nibWithNibName:@"DYCreateSignatureTextCell" bundle:nil] forCellReuseIdentifier:@"DYCreateSignatureTextCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYLotteryDecisionButtonCell" bundle:nil] forCellReuseIdentifier:@"DYLotteryDecisionButtonCell"];
    
    [self.view addSubview:self.toolBar];
    
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight - self.toolBar.frame.size.height);
    [self.view bringSubviewToFront:self.navigationBar];
    
    [self.signatureList addObject:@""];
    [self.signatureList addObject:@""];
    [self.signatureList addObject:@""];
    [self.signatureList addObject:@""];
    
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

#pragma mark - cellDataSource

- (NSArray *)cellDataSource {
    
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
        
        subarr = [NSMutableArray arrayWithCapacity:1];

        dict =  @{@"class":DYCreateSignatureTextCell.class,
                  @"height":@([DYCreateSignatureTextCell getCellFrame:[NSNumber numberWithFloat:66]]),
                  @"data":[DYCreateSignatureTextCellModel ittemModelWithTitle:@"题目" content:_signatureTitle tip:@"请输入决定内容" index:0],
                  @"delegate":@YES};
        [subarr addObject:dict];
        
        [self.signatureList enumerateObjectsUsingBlock:^(NSString *content, NSUInteger idx, BOOL * _Nonnull stop) {
            dict =  @{@"class":DYCreateSignatureTextCell.class,
                      @"height":@([DYCreateSignatureTextCell getCellFrame:[NSNumber numberWithFloat:66]]),
                      @"data":[DYCreateSignatureTextCellModel ittemModelWithTitle:[NSString stringWithFormat:@"选项%ld",idx + 1] content:content tip:@"请输入抽签签文" index:idx],
                      @"delegate":@YES};
            [subarr addObject:dict];
        }];
        
        [arr addObject:subarr];

        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //[self refreshData];
}

#pragma mark DYCreateSignatureTextCellDelegate 题目 签文

- (void)createSignatureTextCell:(DYCreateSignatureTextCell *)cell textField:(UITextField *)textField {
    if ([cell.model.title isEqualToString:@"题目"]) {
        _signatureTitle = textField.text;
    } else {
        [self.signatureList replaceObjectAtIndex:cell.model.index withObject:textField.text];
    }
    self.dataSource = nil;
}

- (void)createSignatureTextCell:(DYCreateSignatureTextCell *)cell buttonAction:(UIButton *)sender {
    if (self.signatureList.count > 1) {
        [self.signatureList removeObjectAtIndex:cell.model.index];
        [self refreshData];
    } else {
        [self showProgressHUD:@"必须保留一个选项"];
    }
}

#pragma mark - NSNotification

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification {
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;   //height 就是键盘的高度
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight - height);
}
 
//当键盘退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification {
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight - self.toolBar.frame.size.height);
}


#pragma mark - goto

//签文
- (void)gotoSignatureViewController {
    DYSignatureViewController *vc = [DYSignatureViewController new];
    vc.signatureTitle = _signatureTitle;
    vc.signatureList = self.signatureList;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载

- (NSMutableArray *)signatureList {
    if (!_signatureList) {
        _signatureList = [NSMutableArray arrayWithCapacity:10];
    }
    return _signatureList;
}

//底部工具条
- (DYCreateSignatureToolBar *)toolBar {
    if (!_toolBar) {
        _toolBar = [DYCreateSignatureToolBar instanceView];
        _toolBar.frame = CGRectMake(0,kScreenHeight - 200, kScreenWidth,200);
        @weakify(self)
        _toolBar.buttonAction = ^(DYCreateSignatureToolBar *toolBar, UIButton *sender) {
            @strongify(self);
            if (sender.tag == 10) {
                [self.signatureList addObject:@""];
                [self refreshData];
            } else if (sender.tag == 11) {
                __block NSString *erro = @"";
                __block BOOL isHave = NO;
                [self.signatureList enumerateObjectsUsingBlock:^(NSString *content, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (content.length > 0) {
                        isHave = YES;
                    }
                }];
                
                if (!isHave) {
                    erro = @"请输入抽签签文";
                }
                
                if (_signatureTitle.length == 0) {
                    erro = @"请输入决定内容";
                }
                
                if (erro.length > 0) {
                    [self showProgressHUD:erro];
                } else {
                    [self gotoSignatureViewController];
                }
            }
        };
        [self.view addSubview:_toolBar];
    }
    return _toolBar;
}

@end
